 package com.example.administrator.nurse.base;

 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
 import android.text.TextUtils;
 import android.util.DisplayMetrics;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.RelativeLayout;
 import android.widget.TextView;
 import android.widget.Toast;

 import com.example.administrator.nurse.NetWork.RequestCallback;
 import com.example.administrator.nurse.NetWork.RequestManager;
 import com.example.administrator.nurse.NetWork.internetHelper;
 import com.example.administrator.nurse.R;
 import com.example.administrator.nurse.common;
 import com.example.administrator.nurse.present.LoginActivity;
 import com.example.administrator.nurse.util.FontHelper;
 import com.example.administrator.nurse.util.LoadingDialog;
 import com.loopj.android.http.RequestParams;

 import org.json.JSONException;
 import org.json.JSONObject;

 /**
 * Created by Administrator on 2016/3/30.
 *
 */

public abstract class BaseActivity extends AppCompatActivity implements RequestCallback{

    private LoadingDialog loadingDialog;
    private final int MAIN_TYPE=0;
    private final int COMMON_TYPE=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setLayotId());
        FontHelper.changeFonts((ViewGroup) getRootView(this), BaseActivity.this);
    }
    public void t(String str){
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    protected View getRootView(Activity activity){
        return ((ViewGroup)activity.findViewById(android.R.id.content)).getChildAt(0);
    }
    public View f(int viewId){
        return findViewById(viewId);
    }

     public float toDip(Activity activity, float value){
         DisplayMetrics dm=activity.getResources().getDisplayMetrics();//获取屏幕参数
         return value*dm.density;
     }

    protected void startOtherActivity(Context context,Class<?> className){
        Intent intent=new Intent(context,className);
        startActivity(intent);
    }

     protected void startOtherActivity(Context context,Class<?> className,Bundle data){
         Intent intent=new Intent(context,className);
         intent.putExtras(data);
         startActivity(intent);
     }
    public static String getJSONValue(JSONObject jsonObject,String key) throws JSONException {
        return jsonObject.isNull(key)?"移动护士":jsonObject.getString(key);
    }

     public static int getJSONINTValue(JSONObject jsonObject,String key) throws JSONException {
         return jsonObject.isNull(key)?0:jsonObject.getInt(key);
     }

    protected abstract int setLayotId();

    protected void configMainHeader(final Activity act,String title){
        RelativeLayout header= (RelativeLayout) act.findViewById(R.id.header);
        TextView titleTV= (TextView) header.findViewById(R.id.title);
        titleTV.setText(title);
        titleTV.setTextColor(getResources().getColor(R.color.nav_color));
        ImageView config= (ImageView) header.findViewById(R.id.config);
        config.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(act, LoginActivity.class);//退出登录
                startActivity(intent);
            }
        });
    }

    protected void configMainHeader(final Activity act,String title,View.OnClickListener onClickListener){
        RelativeLayout header= (RelativeLayout) act.findViewById(R.id.header);
        TextView titleTV= (TextView) header.findViewById(R.id.title);
        titleTV.setText(title);
        titleTV.setTextColor(getResources().getColor(R.color.nav_color));
        ImageView config= (ImageView) header.findViewById(R.id.config);
        config.setOnClickListener(onClickListener);
    }

     //公共标题处理
    protected void configCommonHeader(final Activity act,String title,int type){
        RelativeLayout header= (RelativeLayout) act.findViewById(R.id.commonheader);
        TextView titleTV= (TextView) header.findViewById(R.id.title);
        titleTV.setText(title);
        titleTV.setTextColor(getResources().getColor(R.color.nav_color));
        ImageView back= (ImageView) findViewById(R.id.back_btn);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if(type==1){
            f(R.id.scan).setVisibility(View.GONE);
        }else if(type==0){

        }
    }
    protected abstract void onFailure(int requestCode,Object object, String errorMsg);

    protected abstract void onSuccess(int requestCode,Object object, String response);

    @Override
    public void onFailureCallback(int requestCode, Object object, String errorMsg) {
        onFailure(requestCode,object,errorMsg);
    }

    @Override
    public void onSuccessCallback(int requestCode, Object object, String response) {
        onSuccess(requestCode, object, response);
    }

    public void startLoadingDialog() {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog.createDialog(this);
            //loadingDialog.show();
        }
        loadingDialog.show();
    }

    public void stopLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
        loadingDialog = null;
    }

    public void doGet(String url) {
        doGet(-1, url);
    }

    public void doGet(String url, RequestParams params) {
        doGet(-1, url, params);
    }

    public void doGet(int requestCode, String url) {
        doGet(requestCode, url, null);
    }

    public void doGet(int requestCode, String url, RequestParams params) {
        if (TextUtils.isEmpty(url)) {
            throw new RuntimeException("");
        }
        if(internetHelper.checkConnection()){
            RequestManager.get(requestCode,null, url, params, this);
        }else {
            common.Toast("无法连接网络!");
        }

    }

    public void doPost(String url, RequestParams params) {
        doPost(-1,null, url, params);
    }

    public void doPost(int requestCode,Object object, String url, RequestParams params) {
        if (TextUtils.isEmpty(url)) {
            throw new RuntimeException("");
        }
        if(internetHelper.checkConnection()){
            RequestManager.post(requestCode,object, url, params, this);
        }else {
            common.Toast("无法连接网络!");
        }
    }

}
